package org.exez.modules.visitor.util;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.exez.modules.visitor.constant.Enum;
import org.exez.modules.visitor.vo.PolicePayVO;


public class MinaClient {

	public static void main(String[] args) {

		// TODO Auto-generated method stub
		IoConnector connector = new NioSocketConnector();
		connector.setConnectTimeoutMillis(30000);
		TextLineCodecFactory lineCodec=new TextLineCodecFactory(Charset.forName("UTF-8"));
		lineCodec.setDecoderMaxLineLength(1024*1024*1024); //1G
		lineCodec.setEncoderMaxLineLength(1024*1024*1024); //1G
		connector.getFilterChain().addLast("codec",
                new ProtocolCodecFilter(lineCodec));
		String payId="1001";
		String secret="002f0c86af67473aa280c8d5ef165a19";
		JSONObject json=new JSONObject();
		json.put("method", "connectCheck");
		JSONObject data=new JSONObject();
		long time=System.currentTimeMillis();
		data.put("payId", payId);
		data.put("time", time);
		try {
			data.put("sign", MD5.md5(secret+time));
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		json.put("data", data);
		connector.setHandler(new MinaClientHandle(json.toString()));
		ConnectFuture dd=connector.connect(new InetSocketAddress("122.228.181.162", 9999));
		try {
			Thread.sleep(Integer.parseInt(Enum.PolicePayTime.getName()));
			if(dd.getSession() == null){
				dd=connector.connect(new InetSocketAddress("122.228.181.162", 9999));
				Thread.sleep(Integer.parseInt(Enum.PolicePayTime.getName()));
			}
			if(dd.getSession() == null){
				dd=connector.connect(new InetSocketAddress("122.228.181.162", 9999));
				Thread.sleep(Integer.parseInt(Enum.PolicePayTime.getName()));
			}
			if(dd.getSession() == null){
				dd=connector.connect(new InetSocketAddress("122.228.181.162", 9999));
				Thread.sleep(Integer.parseInt(Enum.PolicePayTime.getName()));
			}
			if(dd.getSession() == null){
				dd=connector.connect(new InetSocketAddress("122.228.181.162", 9999));
				Thread.sleep(Integer.parseInt(Enum.PolicePayTime.getName()));
			}
			if(dd.getSession() == null){
				dd=connector.connect(new InetSocketAddress("122.228.181.162", 9999));
				Thread.sleep(Integer.parseInt(Enum.PolicePayTime.getName()));
			}
			if(dd.getSession() == null){
				dd=connector.connect(new InetSocketAddress("122.228.181.162", 9999));
				Thread.sleep(Integer.parseInt(Enum.PolicePayTime.getName()));
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		json=new JSONObject();
		json.put("method", "getUserInfo");
		data=new JSONObject();
		data.put("pageIndex", 1);
		data.put("pageSize", Enum.PolicePayNum.getName());
		json.put("data", data);
		IoSession ii  = dd.getSession();
		ii.write(json.toString());
	}

}
